/*$Id: IdeaCleanMojo.java 312 2011-04-24 20:03:44Z vip.delete $*/

/**
 * Copyright vip_delete
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.ideaplugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import java.io.File;

/**
 * @author vip_delete
 * @goal clean
 * @since 28.07.2010
 * @threadSafe
 */
public class IdeaCleanMojo extends AbstractMojo
{
    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException
    {
        deleteFile("ipr");
        deleteFile("iws");
        deleteFile("iml");
    }

    private void deleteFile(String extension) throws MojoFailureException
    {
        File file = new File(project.getBasedir(), project.getArtifactId() + "." + extension);
        if (file.exists() && !file.isDirectory())
            if (!file.delete())
                throw new MojoFailureException("Can't delete file: " + file.getAbsolutePath());
    }
}
